﻿using Pay.Data;
using Pay.IService;
using Senparc.NeuChar.Context;
using Senparc.NeuChar.Entities;
using Senparc.NeuChar.Entities.Request;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageHandlers;
using System.IO;

namespace Pay.Token.Utils {
    /// <summary>
    /// 微信公众号消息处理
    /// </summary>
    public class WXPubMessageHandler : MessageHandler<MessageContext<IRequestMessageBase, IResponseMessageBase>> {
        private IWXVerificationCodeService _iWXVerificationCodeService;
        private ISettingService _iSettingService;
        public WXPubMessageHandler(Stream inputStream, PostModel postModel, IWXVerificationCodeService iWXVerificationCodeService, ISettingService iSettingService, int maxRecordCount = 0)
            : base(inputStream, postModel, maxRecordCount) {
            _iWXVerificationCodeService = iWXVerificationCodeService;
            _iSettingService = iSettingService;
        }
        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage) {
            //var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
            //responseMessage.Content = "Hello 拓垦";
            //return responseMessage;

            //不回复任何消息：
            return new SuccessResponseMessage();
        }

        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage) {
            var defaultResponseMessage = base.CreateResponseMessage<ResponseMessageText>();

            var requestHandler =
                requestMessage.StartHandler()
                //关键字不区分大小写，按照顺序匹配成功后将不再运行后面的逻辑
                .Keyword("领取马力", () =>
                {
                    var openId = requestMessage.FromUserName;
                    WXVerificationCode wxVerificationCode = _iWXVerificationCodeService.GetOrAddWXCode(openId);
                    defaultResponseMessage.Content = string.Format("您的验证码为：{0}", wxVerificationCode.VerificationCode);
                    return defaultResponseMessage;
                });

            return requestHandler.GetResponseMessage() as IResponseMessageBase;
        }


        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage) {
            IResponseMessageBase reponseMessage = null;
            //菜单点击，需要跟创建菜单时的Key匹配
            switch (requestMessage.EventKey) {
                case "ServiceTel": {
                        var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
                        reponseMessage = strongResponseMessage;
                        var servicePhone = _iSettingService.GetTokenSetting().app.service_phone;
                        strongResponseMessage.Content = string.Format("您好，拓垦客服热线：{0}  服务时间：周一至周日9:00-18:00（法定假期服务时间将另作安排）欢迎咨询！", servicePhone);
                    }
                    break;

            }
            return reponseMessage;

        }

    }
}